package com.sx.micowell.framework.model

import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.CacheDiskUtils
import com.example.mvvm.base.data.MutableLiveListData
import com.example.mvvm.base.data.SingleLiveEvent
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.SPUtils
import com.miloyu.modelnet.cache.CacheBean
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.MemberSetMealVO
import com.miloyu.modelnet.response.MemberSetMealWrap
import com.miloyu.modelnet.response.UserInfoVO
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.binding.BindingCommand
import com.sx.micowell.framework.binding.impl.IBindingAction

/**
 * @Author: YY
 * @Time: 2021/8/19
 * @Describe TODO
 */
class MemberModel : BaseViewModel() {

    var memberSetMeal: MemberSetMealVO? = null

    val memberSetMealEvent : MutableLiveListData<MemberSetMealVO> = MutableLiveListData()

    // 开通
    val openOnClickCommand = BindingCommand<Any>(IBindingAction {

        val bundle = Bundle()
        bundle.putSerializable(IntentKey.DATA, memberSetMeal)
        context.startContainerActivity(RouteManager.getInstance()
            .getPaymentListFragment(memberSetMeal).javaClass.canonicalName, bundle)
    })

    fun memberSetMeal() {
        HttpClient.instance.apiService
            .memberSetMeal()
            .compose(RxUtils.compose())
            .subscribe(object : ApiResponse<MemberSetMealWrap>() {

                override fun success(data: MemberSetMealWrap?, msg: String?) {
                    LogUtils.d("memberSetMeal: ${data?.info?.size}")
                    memberSetMealEvent.addData(data?.info)
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                    LogUtils.d("memberSetMeal: $errorMsg")
                }

            })
    }


    var userInfoEvent :MutableLiveData<UserInfoVO> = MutableLiveData<UserInfoVO>()
    fun userInfo() {

        addSubscribe(HttpClient.instance.apiService
            .userInfo()
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<UserInfoVO>() {

                override fun success(data: UserInfoVO?, msg: String?) {
                    data?.let {
                        userInfoEvent.value = it
                        // 本地数据缓存
                        CacheDiskUtils.getInstance().put(
                            AppConstant.CacheKey.CACHE_USER_INFO, it,
                            AppConstant.CacheKey.CACHE_SAVE_TIME_SECONDS)
                        ui.refreshEvent.value = true
                        SPUtils.getInstance().put(SPKey.SP_VIP, it.isVip)
                    }

                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                    //ToastUtils.showShort("$errorMsg")
                    ui.refreshEvent.value = false
                }

            })
        )

    }
}